<?php
/**
 * crcl/base
 *
 * Copyright 2013 CRCL - André Sabosch - info@crcl.de
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace  crcl\base;

interface IMvc
{
  /**
   * Runs the application
   */
  public static function run();

  /**
   * Activate Asset Mode
   */
  public static function activateAssets($bAssets = \TRUE);

  /**
   * Check asset mode active
   */
  public static function isAssets();

  /**
   *  Active Rest Mode
   * use rest/error for invalid requests
   */
  public static function activateRest($bRest = \TRUE);

  /**
   * Check Rest Mode
   */
  public static function isRest();
}

class Mvc implements IMvc
{
  public static $bAssets = \FALSE;
  public static $bRest = \FALSE;
  public static $sTheme = \NULL;

  public static function run()
  {
    $oRequest = mvc\Request::getInstance(self::evaluateRequest());
    $oRouter = mvc\Router::getInstance(self::evaluateRouter());
    $oResponse = mvc\Response::getInstance(self::evaluateResponse());

    $oFrontcontroller = new mvc\Frontcontroller();
    $oFrontcontroller->setRequest($oRequest);
    $oFrontcontroller->setRouter($oRouter);
    $oFrontcontroller->setResponse($oResponse);
    $oFrontcontroller->run();
  }

  public static function activateAssets($bAssets = \TRUE)
  {
    self::$bAssets = $bAssets;
  }

  public static function isAssets()
  {
    return self::$bAssets;
  }

  public static function activateRest($bRest = \TRUE)
  {
    self::$bRest = $bRest;
  }

  public static function isRest()
  {
    return self::$bRest;
  }

  public static function setTheme($sTheme)
  {
    self::$sTheme = $sTheme;
  }

  public static function hasTheme()
  {
    $bReturn = \FALSE;
    if (!\is_null(self::$sTheme))
    {
      $bReturn = \TRUE;
    }
    return $bReturn;
  }

  public static function getTheme()
  {
    return self::$sTheme;
  }

  /** PRIVATE *************************************************************************************/

  private static function evaluateRequest()
  {
    if (\crcl\base\Console::isCli())
    {
      return mvc\Request::TYPE_CONSOLE;
    }
    else
    {
      //tested with external call
      //@codeCoverageIgnoreStart
      return mvc\Request::TYPE_HTTP;
      //@codeCoverageIgnoreEnd
    }
  }

  private static function evaluateRouter()
  {
    if (self::$bRest)
    {
      return mvc\Router::TYPE_REST;
    }

    if (isset($_SERVER['HTTP_ACCEPT']))
    {
      if (\strpos($_SERVER['HTTP_ACCEPT'], 'vnd.') !== \FALSE)
      {
        return mvc\Router::TYPE_REST;
      }
    }

    if (\crcl\base\Console::isCli())
    {
      return mvc\Router::TYPE_CONSOLE;
    }
    //tested with external call
    //@codeCoverageIgnoreStart
    return mvc\Router::TYPE_HTTP;
    //@codeCoverageIgnoreEnd
  }

  private static function evaluateResponse()
  {
    if (\crcl\base\Console::isCli())
    {
      return mvc\Response::TYPE_CONSOLE;
    }
    else
    {
      //tested with external call
      //@codeCoverageIgnoreStart
      return mvc\Response::TYPE_HTTP;
      //@codeCoverageIgnoreEnd
    }
  }
}